//
//  MainTabBar.swift
//  LDMicroblog07
//
//  Created by pro on 15/9/16.
//  Copyright © 2015年 pro. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    private let itemsCount = 4
    
    //懒加载 加号 按钮
    lazy var plusBtn: UIButton = {
    
        let btn = UIButton(type: UIButtonType.Custom)
        //背景
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //前景
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到父视图
        // self. 闭包，提前准备好的代码，在需要的时候执行
        self.addSubview(btn)
        
        return btn
    }()

    override func layoutSubviews() {
        super.layoutSubviews()
//        print(__FUNCTION__)
        let w = bounds.width / CGFloat(itemsCount + 1)
        let h = bounds.height
        
        let tempF = CGRectMake(0, 0, w, h)
        
        var index = 0
        for v in subviews {
        
            //此处需要判断是不是按钮，防止在主控制器中 添加加号点击事件时，子控件排布混乱
            if v is UIControl && !(v is UIButton){
            
                v.frame = CGRectOffset(tempF, w * CGFloat(index), 0)
                index++
                if index == 2 {//留出中间位置给加号按钮
                
                    index++
                }
            }
        }
        //加号按钮
        plusBtn.frame = CGRectOffset(tempF, w * 2, 0)
        
        
    }
    
    

}
